package com.example.mvc.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(TokenInterceptor.class);
    private static final String TOKEN = "token";
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        String token = request.getHeader(TOKEN);
        if (StringUtils.hasText(token)) {
            log.info("read token from header");
            return true;
        }

        token = request.getParameter(TOKEN);
        if (StringUtils.hasText(token)) {
            log.info("read token from request");
            return true;
        }

        // validate token

        return false;
    }
}
